package com.ignou.mca.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.ignou.mca.beans.MailBean;

public class MailUtilities {
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String emailFromAddress = "info.onlineexam@gmail.com";
	private static final String emailFromPassword = "OES@Java";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	public static void sendMail(String purpose, MailBean mb)
			throws AddressException, MessagingException {
		boolean debug = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(emailFromAddress,
								emailFromPassword);
					}
				});

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(emailFromAddress);
		msg.setFrom(addressFrom);

		String[] recipients = new String[1];
		recipients[0] = mb.getEmail();

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		if (purpose.equalsIgnoreCase("register")) {
			msg
					.setSubject("[Online Examination System]:Your New Account Detalis");
			// Set message content
			msg
					.setText("Hi "
							+ mb.getLastName()
							+ ", \nYou've been successfully registered for Online Examination System \nUserId: "
							+ mb.getUserName()
							+ "\nPassWord: "
							+ mb.getPassword()
							+ "\n"
							+ "Please click below link to activate your account:\n"
							+ "" + mb.getActivationLink() + "?activationCode="
							+ mb.getRandomNumber() + "&emailID='"
							+ mb.getEmail() + "'&userName='" +mb.getUserName() +"'\n"
							+ " Please feel free to send your comments to : "
							+ emailFromAddress);
		} 
		
		
		else if (purpose.equalsIgnoreCase("useractive")) 
		{
			msg.setSubject("[Online Examination System]:Your New Account Activated successfully");
			// Set message content
			msg.setText("Hi "
					+ mb.getUserName()
					+ "Your user name and password is activated and now you can login to Online examination system"
					+ "\n Please feel free to send your comments to : "
					+ emailFromAddress);
		}
		else if (purpose.equalsIgnoreCase("scheduleexam")) {
			msg.setSubject("[Online Examination System]: Thank You for Voting");
			// Set message content
			msg.setText("Hi " + mb.getLastName()
					+ ", \nYou've been scheduled for " + mb.getSubjectName()
					+ "exam. Please find the details below:\n " + "Subject:"
					+ mb.getSubjectName() + "Date Registration:" + mb.getExamDate()
					+ "Fees Paid:" + mb.getFeesPaid()
					+ "\n Tell us your feedback at " + emailFromAddress);

		} else if (purpose.equalsIgnoreCase("examresult")) {
			msg.setSubject("[Online Examination System]: Exam Result");
			// Set message content
			msg.setText("Hi " + mb.getLastName()
					+ ", \n Thank you for using Online Examination System\n"
					+ "Please find below your result:\n" + "Subject: "
					+ mb.getSubjectName() + "\nExam completion date:"
					+ mb.getExamDate() + "\nScore: " + mb.getScore()
					+ "\nExam Result: " + mb.getExamResult());
		} else {
			msg.setSubject("[Online Examination System]: Forgot Password");
			// Set message content
			msg.setText("Hi,"+mb.getUserName()+" \nYour Password is : "
					+ mb.getPassword());

		}
		Transport.send(msg);
	}
}
